unit Setting;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TSetting_Form = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    host_text: TEdit;
    database_text: TEdit;
    user_text: TEdit;
    password_text: TEdit;
    protocol_text: TEdit;
    savebtn: TButton;
    cancelbtn: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cancelbtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure init();
    procedure savebtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    protocol, database, host, user, password: string;
    constructor Create();               overload;

  end;

var
  Setting_Form: TSetting_Form;

implementation

{$R *.dfm}

constructor TSetting_Form.Create();
begin
  init();
end;
procedure TSetting_Form.init();
var mfile:textfile;
    read:string;
    list:Tstringlist;
  begin

    if(FileExists(ExtractFilePath(Application.ExeName)+'settings.txt'))then
    begin
      AssignFile(mfile,ExtractFilePath(Application.ExeName)+'settings.txt');
      Reset(mfile);
      list:=tstringlist.Create;
      try
        Readln(mfile, read);
        ExtractStrings(['['], [], PChar(read), list);
        protocol:=copy(list[1], 0,length(list[1])-1);
        list:=tstringlist.Create;
        Readln(mfile, read);
        ExtractStrings(['['], [], PChar(read), list);
        host:=copy(list[1], 0,length(list[1])-1);
        Readln(mfile, read);
        list:=tstringlist.Create;
        ExtractStrings(['['], [], PChar(read), list);
        database:=copy(list[1], 0,length(list[1])-1);
        Readln(mfile, read);
        list:=tstringlist.Create;
        ExtractStrings(['['], [], PChar(read), list);
        user:=copy(list[1], 0,length(list[1])-1);
        Readln(mfile, read);
        list:=tstringlist.Create;
        ExtractStrings(['['], [], PChar(read), list);
        password:=copy(list[1], 0,length(list[1])-1);
        CloseFile(mfile);

      finally
      FreeAndNil(list);
      end;

    end
    else
    begin
      protocol:='mysql';
      database:='dpt';
      user:='root';
      password:='';
      host:='localhost';
       AssignFile(mfile, ExtractFilePath(Application.ExeName)+'settings.txt');
       Rewrite(mfile);
       Writeln(mfile,'protocol: ['+protocol+']');
       Writeln(mfile,'host: ['+host+']');
       Writeln(mfile,'database: ['+database+']');
       Writeln(mfile,'user: ['+user+']');
       Writeln(mfile,'password: ['+password+']');
       CloseFile(mfile);

    end;

end;
procedure TSetting_Form.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TSetting_Form.cancelbtnClick(Sender: TObject);
begin
  close;
end;

procedure TSetting_Form.FormCreate(Sender: TObject);
begin
  BorderIcons := BorderIcons - [biMaximize];
  init;

  protocol_text.Text:=protocol;
  database_text.Text:=database;
  user_text.Text:=user;
  password_text.Text:=password;
  host_text.Text:=host;

end;

procedure TSetting_Form.savebtnClick(Sender: TObject);
var mfile:textfile;
begin
  protocol:=protocol_text.Text;
  database:=database_text.Text;
  user:=user_text.Text;
  password:=password_text.Text;
  host:=host_text.Text;

  AssignFile(mfile, ExtractFilePath(Application.ExeName)+'settings.txt');
  Rewrite(mfile);
  Writeln(mfile,'protocol: ['+protocol+']');
  Writeln(mfile,'host: ['+host+']');
  Writeln(mfile,'database: ['+database+']');
  Writeln(mfile,'user: ['+user+']');
  Writeln(mfile,'password: ['+password+']');
  CloseFile(mfile);
  Showmessage('Pengaturan berhasil disimpan');
end;

end.
